package cn.wolfcode.rbac.springSecurity.detail;

import cn.wolfcode.rbac.domain.Employee;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Getter
@Setter
public class MyUserDetails implements UserDetails {

    private Employee employee;

    //注意：如果在当前类中查权限的话，但是该类不是一个Spring管理的类,因此不能依赖注入其他的类
    private List<GrantedAuthority> expressions;

    public MyUserDetails() {
    }

    public MyUserDetails(Employee employee,List<String> expressions) {
        this.employee = employee;
        if(!CollectionUtils.isEmpty(expressions)){
             this.expressions = expressions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
        }
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return expressions;
    }

    @Override
    public String getPassword() {
        return employee.getPassword();
    }

    @Override
    public String getUsername() {
        return employee.getName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
